'use strict';

const BaseService = require('./base');

class ReplyService extends BaseService {
  get _model() {
    return 'Reply';
  }

  async findOne({ id }) {
    const { app } = this;
    const where = this.formatWhere({ exact: { id } });
    const include = [
      {
        model: app.model.User,
        attributes: {
          exclude: app.exclude.concat([ 'password' ]),
        },
        include: [
          {
            model: app.model.UserInfo,
            attributes: [ 'avatar' ],
          },
        ],
      },
      {
        model: app.model.House,
        attributes: {
          exclude: app.exclude,
        },
        include: [
          {
            model: app.model.Area,
            attributes: [ 'name' ],
          },
          {
            model: app.model.User,
            as: 'Owner',
            attributes: {
              exclude: app.exclude.concat([ 'password' ]),
            },
            required: false,
            include: [
              {
                model: app.model.UserInfo,
                attributes: [ 'avatar' ],
              },
            ],
          },
        ],
      },
      {
        model: app.model.Comment,
        attributes: {
          exclude: app.exclude,
        },
        include: [
          {
            model: app.model.User,
            attributes: {
              exclude: app.exclude.concat([ 'password' ]),
            },
            required: false,
            include: [
              {
                model: app.model.UserInfo,
                attributes: [ 'avatar' ],
              },
            ],
          },
        ],
      },
      {
        model: app.model.Reply,
        as: 'BeReply',
        attributes: {
          exclude: app.exclude,
        },
        include: [
          {
            model: app.model.User,
            attributes: {
              exclude: app.exclude.concat([ 'password' ]),
            },
            required: false,
            include: [
              {
                model: app.model.UserInfo,
                attributes: [ 'avatar' ],
              },
            ],
          },
        ],
      },
    ];
    return this._findOne({ where, include });
  }

  async create({ content, houseId, cid, uid, rid, status }) {
    return this._create({
      content, houseId, cid, uid, rid, status,
    });
  }

  async update({ status, id }) {
    const where = { id };
    const body = { status };
    return this._update({ body, where });
  }

  async destory(id) {
    const where = { id };
    return this._destory({ where });
  }

  async findList({ houseId, cid, uid, status, replyStatus, pageIndex, pageSize, createdAt, prop, sort }) {
    const { app } = this;
    const where = this.formatWhere({ range: { createdAt }, exact: { status, houseId, cid } });
    const whereReply = this.formatWhere({ exact: { status: replyStatus } });
    const order = this.formatOrder(prop, sort);
    const include = [
      {
        model: app.model.User,
        attributes: [ 'nickName' ],
        include: [],
      },
    ];
    if (houseId || cid) {
      include[0].include.push({
        model: app.model.UserInfo,
        attributes: [ 'avatar' ],
      });
      include.push({
        model: app.model.Reply,
        as: 'ToReply',
        required: false,
        attributes: {
          exclude: app.excludeDate,
        },
        where: whereReply,
        include: [
          {
            model: app.model.User,
            attributes: [ 'nickName' ],
            include: [
              {
                model: app.model.UserInfo,
                attributes: [ 'avatar' ],
              },
            ],
          },
        ],
      });
    }
    if (uid) {
      include.pop();
      include.push({
        model: app.model.House,
        attributes: [ 'name' ],
        include: [
          {
            model: app.model.HouseInfo,
            attributes: [ 'pictures' ],
            where: {
              status: 3,
            },
          },
        ],
      });
      include.push({
        model: app.model.Comment,
        attributes: [ 'title', 'pictures' ],
      });
      include.push({
        model: app.model.Reply,
        as: 'BeReply',
        attributes: [ 'content' ],
        include: [
          {
            model: app.model.House,
            attributes: [ 'id' ],
          },
          {
            model: app.model.Comment,
            attributes: [ 'id' ],
          },
        ],
      });
    }
    return this._findList({ pageIndex, pageSize, where, order, include });
  }
}

module.exports = ReplyService;
